package com.congee02.dbservlet.framework.jdbc;

import com.congee02.dbservlet.framework.orm.ResultSetConverter;
import com.congee02.dbservlet.framework.orm.SqlGenerator;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class ObjectJdbcTemplate<T> {

    private final JdbcTemplate template;                    // 执行 SQL
    private final SqlGenerator<T> sqlGenerator;             // 生成 SQL
    private final ResultSetConverter<T> resultSetConverter; // 转化为对象

    ObjectJdbcTemplate(Class<T> tableEntityClazz, String configPropertiesFilepath) throws IOException {
        this.template = JdbcTemplateFactory.getJdbcTemplateFromPropertiesFile(configPropertiesFilepath);
        this.sqlGenerator = new SqlGenerator<>(tableEntityClazz);
        this.resultSetConverter = new ResultSetConverter<>(tableEntityClazz);
    }

    ObjectJdbcTemplate(JdbcTemplate template, Class<T> tableEntityClazz) {
        this.template = template;
        this.sqlGenerator = new SqlGenerator<>(tableEntityClazz);
        this.resultSetConverter = new ResultSetConverter<>(tableEntityClazz);
    }

    public static <T> ObjectJdbcTemplate<T> createObjectJdbcTemplate(Class<T> tableEntityClazz, JdbcTemplate template) {
        return new ObjectJdbcTemplate<>(template, tableEntityClazz);
    }

    @SafeVarargs
    public final String insertSql(T... entities) {
        return sqlGenerator.insertSql(entities);
    }
    @SafeVarargs
    public final int insert(T... entities) throws SQLException {
        String sql = insertSql(entities);
        return template.executeUpdate(sql);
    }

    @SafeVarargs
    public final String deleteSql(T... conditions) {
        return sqlGenerator.deleteSql(conditions);
    }
    @SafeVarargs
    public final int delete(T... conditions) throws SQLException {
        String sql = deleteSql(conditions);
        return template.executeUpdate(sql);
    }

    @SafeVarargs
    public final String updateSql(T entity, T... condition) {
        return sqlGenerator.updateSql(entity, condition);
    }
    @SafeVarargs
    public final int update(T entity, T... condition) throws SQLException {
        String sql = updateSql(entity, condition);
        return template.executeUpdate(sql);
    }

    @SafeVarargs
    public final List<T> select(T... condition) throws SQLException {
        String sql = sqlGenerator.selectSql(condition);
        ResultSet resultSet = template.executeQuery(sql);
        return resultSetConverter.convert(resultSet);
    }

    @SafeVarargs
    public final List<T> selectByPage(int pageIndex, int pageSize, T... conditions) throws SQLException {
        if (pageIndex < 1) {
            throw new RuntimeException("PageIndex starts from 1");
        }
        if (pageSize < 1) {
            throw new RuntimeException("PageSize must be positive");
        }
        String sql = sqlGenerator.selectSqlByPage(pageIndex, pageSize, conditions);
        ResultSet resultSet = template.executeQuery(sql);
        return resultSetConverter.convert(resultSet);
    }

    public final int[] executeBatch(String... sqlArray) throws SQLException {
        return template.executeBatch(sqlArray);
    }

    public final int[] executeBatch(List<String> sqlList) throws SQLException {
        return template.executeBatch(sqlList);
    }

    public void close() throws SQLException {
        template.shutDown();
    }


}
